package com.ruoyi.common.utils;

import com.ruoyi.app.domain.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
@description:
@ClassName RewardUtils
@author chen
@create 2025-03-31 20:24
@Version 1.0
*/
public class RewardUtils
{
    private static final Map<Integer, Map<Integer, Integer>> rewardMap = new HashMap<>();

    static
    {
        Map<Integer, Integer> price49Rewards = new HashMap<>();
        price49Rewards.put(1, 20);
        price49Rewards.put(2, 5);
        price49Rewards.put(3, 0);
        rewardMap.put(49, price49Rewards);

        Map<Integer, Integer> price199Rewards = new HashMap<>();
        price199Rewards.put(1, 80);
        price199Rewards.put(2, 20);
        price199Rewards.put(3, 0);
        rewardMap.put(199, price199Rewards);

        Map<Integer, Integer> price888Rewards = new HashMap<>();
        price888Rewards.put(1, 288);
        price888Rewards.put(2, 90);
        price888Rewards.put(3, 0);
        rewardMap.put(888, price888Rewards);
    }


    // 测试示例
    public static void main(String[] args) {
        // 构建用户推荐关系：A <- B <- C <- D
        User userA = new User("A", null);
        User userB = new User("B", userA);
        User userC = new User("C", userB);
        User userD = new User("D", userC);

        RewardUtils service = new RewardUtils();

        // 测试案例1：B购买49元
//        System.out.println("案例1：B购买49元");
//        service.handlePurchase(userB, 49);
//        System.out.println("A的奖励余额: " + userA.getRewardBalance()); // 应20
//        System.out.println("B的奖励余额: " + userB.getRewardBalance()); // 应0（因为B是购买者，奖励给上级A）

//        // 测试案例2：C购买199元
//        System.out.println("\n案例2：C购买199元");
//        service.handlePurchase(userC, 199);
//        System.out.println("B的奖励余额: " + userB.getRewardBalance()); // 80
//        System.out.println("A的奖励余额: " + userA.getRewardBalance()); // 20（之前20）+ 20 = 40
//
//        // 测试案例3：D购买888元（三级上级：C、B、A）
        System.out.println("\n案例3：D购买888元");
        service.handlePurchase(userD, 888);
        System.out.println("C的奖励余额: " + userC.getRewardBalance()); // 288（层级1）
        System.out.println("B的奖励余额: " + userB.getRewardBalance()); // 80 + 90 = 170（层级2）
        System.out.println("A的奖励余额: " + userA.getRewardBalance()); // 40 + 0（层级3奖励为0） = 40
    }


    // 处理用户购买
    public void handlePurchase(User user, int purchaseAmount)
    {
        List<User> superiors = getSuperiorsUpToThreeLevels(user);

        Map<Integer, Integer> rewards = rewardMap.get(purchaseAmount);
        if (rewards == null) {
            System.out.println("无奖励规则，购买金额无效");
            return;
        }

        for (int i = 0; i < superiors.size(); i++)
        {
            int currentLevel = i + 1;
            Integer rewardAmount = rewards.get(currentLevel);
            if (rewardAmount != null && rewardAmount > 0)
            {
                User superior = superiors.get(i);
                distributeReward(superior, rewardAmount);
                System.out.printf("用户 %s 获得 %d 元奖励（层级 %d）%n", superior.getId(), rewardAmount, currentLevel);
            }
        }
    }


    // 获取上级链，最多三级
    private List<User> getSuperiorsUpToThreeLevels(User user)
    {
        List<User> superiors = new ArrayList<>();
        User currentSuperior = user.getSuperior();
        int level = 1;

        while (currentSuperior != null && level <= 3)
        {
            superiors.add(currentSuperior);
            currentSuperior = currentSuperior.getSuperior();
            level++;
        }

        return superiors;
    }

    // 发放奖励到用户账户
    private void distributeReward(User user, int amount)
    {
        // 这里实现具体的奖励发放逻辑，例如更新用户余额或记录奖励记录
        user.addReward(amount);
    }



}
